<?php
/**
 *
 * User: wesley
 * Date: 2017/11/9 0009
 * Time: 9:34
 * Email: wu93wesley@foxmail.com
 */

namespace app\api\controller;
class User extends Common {
    public function login () {
        /** 接收参数 */
        $data = $this->params;
        /** 检测用户名*/
        $user_name_type = $this->check_username($data[ 'user_name' ]);
        switch ($user_name_type) {
            case    'phone':
                $this->check_exist($data[ 'user_name' ], 'phone', 1);
                $db_res = db('user')
                    ->field('user_id,user_name,user_phone,user_email,user_rtime,user_pwd')
                    ->where('user_phone', $data[ 'user_name' ])
                    ->find();
                break;
            case    'email':
                $this->check_exist($data[ 'user_name' ], 'email', 1);
                $db_res = db('user')
                    ->field('user_id,user_name,user_phone,user_email,user_rtime,user_pwd')
                    ->where('user_email', $data[ 'user_name' ])
                    ->find();
                break;
        }
        if ($db_res[ 'user_pwd' ] !== $data[ 'user_pwd' ]) {
            $this->message(400, '用户名或者密码不正确');
        } else {
            $this->message(200, '登录成功！', $db_res);
        }
    }

    public function register () {
        /** 接收参数*/
        $data = $this->params;
        /** 检测验证码*/
//        $this->check_code($data[ 'user_name' ], $data[ 'code' ]);
        /** 检测用户名*/
        $user_name_type = $this->check_username($data[ 'user_name' ]);
        switch ($user_name_type) {
            case 'phone':
                $this->check_exist($data[ 'user_name' ], 'phone', 0);
                $data[ 'user_phone' ] = $data[ 'user_name' ];
                break;
            case 'email':
                $this->check_exist($data[ 'user_name' ], 'email', 0);
                $data[ 'user_email' ] = $data[ 'user_name' ];
                break;
        }
        /** 将用户信息写入数据库*/
        unset($data[ 'user_name' ]);
        $data[ 'user_rtime' ] = time();
        $res = db('user')->insert($data);
        if (!$res) {
            $this->message(400, '用户注册失败');
        } else {
            $this->message(200, '用户注册成功！');
        }
    }

    public function upload_head_img () {
        /** 接收参数*/
        $data = $this->params;
        /** 上传文件，获取路径*/
        $head_img_path = $this->upload_file($data[ 'user_icon' ], 'head_img');
        /** 存入数据库*/
        $res = db('user')->where('user_id', $data[ 'user_id' ])->setField('user_icon', $head_img_path);
        if ($res) {
            $this->message(200, '头像上传成功', $head_img_path);
        } else {
            $this->message(400, '头像上传失败');
        }
    }

    public function change_pwd () {
        /** 接收参数*/
        $data = $this->params;
        /** 检查用户名并取出数据库中的密码*/
        $user_name_type = $this->check_username($data[ 'user_name' ]);
        switch ($user_name_type) {
            case    'phone':
                $this->check_exist($data[ 'user_name' ], 'phone', 1);
                $where[ 'user_phone' ] = $data[ 'user_name' ];
                break;
            case 'email':
                $this->check_exist($data[ 'user_name' ], 'email', 1);
                $where[ 'user_email' ] = $data[ 'user_name' ];
                break;
        }
        /** 判读原始密码是否正确*/
        $db_ini_pwd = db('user')->where($where)->value('user_pwd');
        if ($db_ini_pwd !== $data[ 'user_ini_pwd' ]) {
            $this->message(400, '原密码错误！');
        }
        /** 把新密码存入数据库*/
        $res = db('user')->where($where)->setField('user_pwd', $data[ 'user_pwd' ]);
        if ($res !== false) {
            $this->message(200, '密码修改成功');
        } else {
            $this->message(400, '密码修改失败');
        }
    }

    /** 找回密码*/
    public function find_pwd () {
        /** 接收参数*/
        $data = $this->params;
        /** 检测验证码*/
        /** bug 待解决*/
//        $this->check_code($data[ 'user_name' ], $data[ 'code' ]);
        /** 检测用户名*/
        $user_name_type = $this->check_username($data[ 'user_name' ]);

        /** 数据库查找用户名，并发送code*/
        switch ($user_name_type) {
            case    'phone':
                $this->check_exist($data[ 'user_name' ], 'phone', 1);
                $where[ 'user_phone' ] = $data[ 'user_name' ];
                break;
            case    'email':
                $this->check_exist($data[ 'user_name' ], 'email', 1);
                $where[ 'user_email' ] = $data[ 'user_name' ];
                break;
        }
        /** 把新密码存入数据库*/
        $res = db('user')->where($where)->setField('user_pwd', $data[ 'user_pwd' ]);
        if ($res !== false) {
            $this->message(200, '密码修改成功');
        } else {
            $this->message(400, '密码修改失败');
        }
    }

    /** 绑定邮箱*/
    public function bind_email () {
        /** 接收参数*/
        $data = $this->params;
        /** 检查验证码*/
//        $this->check_code($data[ 'email' ], $data[ 'code' ]);
        /** 修改数据库*/
        $res = db('user')->where('user_id', $data[ 'user_id' ])->setField('user_phone', $data[ 'email' ]);
        if ($res !== false) {
            $this->message(200, '绑定邮箱成功');
        } else {
            $this->message(400, '绑定邮箱失败');
        }
    }

    /** 绑定手机*/
    public function bind_phone () {
        /** 接收参数*/
        $data = $this->params;
        /** 检查验证码*/
//        $this->check_code($data[ 'phone' ], $data[ 'code' ]);
        /** 修改数据库*/
        $res = db('user')->where('user_id', $data[ 'user_id' ])->setField('user_phone', $data[ 'phone' ]);
        if ($res !== false) {
            $this->message(200, '绑定手机成功');
        } else {
            $this->message(400, '绑定手机失败');
        }
    }

    /** 用户设定昵称*/
    public function set_nickname () {
        /**获取参数*/
        $data = $this->params;
        /** 检测昵称*/
        $res = db('user')->where('user_nickname', $data[ 'user_nickname' ])->find();
        if ($res) {
            $this->message(400, '该昵称已被占用！');
        }
        /** 写入数据库*/
        $res = db('user')->where('user_id', $data[ 'user_id' ])->setField('user_nickname', $data[ 'user_nickname' ]);
        if (!$res) {
            $this->message(400, '修改昵称失败！');
        } else {
            $this->message(200, '修改昵称成功！');
        }
    }
}

